<?php
class Contact_model extends MY_Model {
	private $table = 'contact';

	function getsearchContent($limit,$page){
		$this->db->select('*');
		$this->db->limit($limit,$page);
		$this->db->order_by($this->input->post('func_order_by'),$this->input->post('order_by'));
		if($this->input->post('content')!='' && $this->input->post('content')!='type here...'){
			$this->db->where('(`name` LIKE "%'.trim($this->input->post('content')).'%" OR `name` = "%'.trim($this->input->post('content')).'%")');
			$this->db->or_where('(`class` LIKE "%'.trim($this->input->post('content')).'%" OR `class` = "%'.trim($this->input->post('content')).'%")');
			$this->db->or_where('(`email` LIKE "%'.trim($this->input->post('content')).'%" OR `email` = "%'.trim($this->input->post('content')).'%")');
		}
		if($this->input->post('dateFrom')!='' && $this->input->post('dateTo')==''){
			$this->db->where('created >= "'.date('Y-m-d 00:00:00',strtotime($this->input->post('dateFrom'))).'"');
		}
		if($this->input->post('dateFrom')=='' && $this->input->post('dateTo')!=''){
			$this->db->where('created <= "'.date('Y-m-d 23:59:59',strtotime($this->input->post('dateTo'))).'"');
		}
		if($this->input->post('dateFrom')!='' && $this->input->post('dateTo')!=''){
			$this->db->where('created >= "'.date('Y-m-d 00:00:00',strtotime($this->input->post('dateFrom'))).'"');
			$this->db->where('created <= "'.date('Y-m-d 23:59:59',strtotime($this->input->post('dateTo'))).'"');
		}
		$query = $this->db->get(PREFIX.$this->table);

		if($query->result()){
			return $query->result();
		}else{
			return false;
		}
	}

	function getTotalsearchContent(){
		$this->db->select('*');
		if($this->input->post('content')!='' && $this->input->post('content')!='type here...'){
			$this->db->where('(`name` LIKE "%'.trim($this->input->post('content')).'%" OR `name` = "%'.trim($this->input->post('content')).'%")');
			$this->db->or_where('(`class` LIKE "%'.trim($this->input->post('content')).'%" OR `class` = "%'.trim($this->input->post('content')).'%")');
			$this->db->or_where('(`email` LIKE "%'.trim($this->input->post('content')).'%" OR `email` = "%'.trim($this->input->post('content')).'%")');
		}
		if($this->input->post('dateFrom')!='' && $this->input->post('dateTo')==''){
			$this->db->where('created >= "'.date('Y-m-d 00:00:00',strtotime($this->input->post('dateFrom'))).'"');
		}
		if($this->input->post('dateFrom')=='' && $this->input->post('dateTo')!=''){
			$this->db->where('created <= "'.date('Y-m-d 23:59:59',strtotime($this->input->post('dateTo'))).'"');
		}
		if($this->input->post('dateFrom')!='' && $this->input->post('dateTo')!=''){
			$this->db->where('created >= "'.date('Y-m-d 00:00:00',strtotime($this->input->post('dateFrom'))).'"');
			$this->db->where('created <= "'.date('Y-m-d 23:59:59',strtotime($this->input->post('dateTo'))).'"');
		}
		$query = $this->db->get(PREFIX.$this->table);

		if($query->result()){
			return count($query->result());
		}else{
			return false;
		}
	}

	function getDetailManagement($id){
		$this->db->select('*');
		$this->db->where('id',$id);
		$query = $this->db->get(PREFIX.$this->table);

		if($query->result()){
			return $query->result();
		}else{
			return false;
		}
	}

	function saveManagement(){
		if($this->input->post('statusAdmin')=='on'){
			$status = 1;
		}else{
			$status = 0;
		}

		if($this->input->post('hiddenIdAdmincp')==0){
			$data = array(
				'name'=> $this->input->post('lh_name'),
				'class'=> $this->input->post('lh_class'),
				'email'=> $this->input->post('lh_email'),
				'phone'=> $this->input->post('lh_phone'),
				'content'=> $this->input->post('content'),
				'status'=> 0,
				'created'=> date('Y-m-d H:i:s',time()),
				'updated'=> date('Y-m-d H:i:s',time()),
			);
			if($this->db->insert(PREFIX.$this->table,$data)){
				return true;
			}
		}else{
			//Xử lý xóa hình khi update thay đổi hình
			$result = $this->getDetailManagement($this->input->post('hiddenIdAdmincp'));

			$data = array(
				'name'=> $this->input->post('lh_name'),
				'class'=> $this->input->post('lh_class'),
				'email'=> $this->input->post('lh_email'),
				'phone'=> $this->input->post('lh_phone'),
				'content'=> $this->input->post('content'),
				'status'=> $status,
				'updated'=> date('Y-m-d H:i:s',time()),
			);
			$this->db->where('id',$this->input->post('hiddenIdAdmincp'));
			if($this->db->update(PREFIX.$this->table,$data)){
				return true;
			}
		}
		return false;
	}
    function getCourse()
	{
		$this->db->select('*');
		$this->db->order_by('created','DESC');
		$query = $this->db->get(PREFIX.'course');
		$result = $query->result();

		if($result)
		{
			return $result;
		}else{
			return false;
		}
	}
	function send_contact() {
    $data = array(
    		'title' => trim($this->input->post('co_title')),
        'name' => trim($this->input->post('co_name')),
        'email' => trim($this->input->post('co_email')),
        'phone' => $this->input->post('co_phone'),
        'address' => $this->input->post('co_address'),
        'content' => $this->input->post('co_content'),
        'created' => date('Y-m-d H:i:s', time()),
        'status' => 0
    );

    if ($this->db->insert(PREFIX.$this->table, $data)) {
        return $this->db->insert_id();
    }
    return false;
  }
}
